/*
** Copyright (c) 2008 Mobile GCalendar
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 *
 */
package au.edu.mq.comp.itec800.mgc.lib;

import java.io.IOException;
import java.net.URL;

import au.edu.mq.comp.itec800.mgc.util.StringUtils;
import au.edu.mq.comp.itec800.mgc.util.Utils;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.util.ServiceException;


/**
 * A convenience enum class containing string representations of
 * Google Calendar's remote RSS/Atom control feeds.
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 */
public enum GCFeed
{
  all     ("allcalendars"),
  owned   ("owncalendars"),
  primary ("private");


  private static final String google        = "http://www.google.com/";
  private static final String gcalendar     = google + "calendar/";
  private static final String feeds         = gcalendar + "feeds/";
  private static final String defaultFeed   = feeds + "default/";

  private static final String full          = "full";
  private static final String simple        = "simple";

  private String              access        = null;
  private URL                 fullFeedURL   = null;
  private URL                 simpleFeedURL = null;


  private GCFeed(final String access)
  {
    this.access   = StringUtils.sanitize(access);
    fullFeedURL   = Utils.toUrl(constructFeed(this.access, full));
    simpleFeedURL = Utils.toUrl(constructFeed(this.access, simple));
  }

  private String              constructFeed(final String access,
                                            final String type)
  {
    final StringBuffer        sb = new StringBuffer(128);

    sb.append(defaultFeed);
    sb.append(access);
    sb.append("/");
    sb.append(type);
    return (sb.toString());
  }

  public URL                  getFullUrl()
  {
    return (fullFeedURL);
  }

  public URL                  getSimpleUrl()
  {
    return (simpleFeedURL);
  }

  public static URL           getCustom(final String id)
  {
    return (Utils.toUrl(feeds + StringUtils.sanitize(id)));
  }

  public static CalendarFeed  getServiceFeed(final CalendarService gcs,
                                             final URL feedUrl)
    throws IOException, ServiceException
  {
    CalendarFeed              feed  = null;

    if ((gcs != null) && (feedUrl != null))
    {
      feed = gcs.getFeed(feedUrl, CalendarFeed.class);
    }
    return (feed);
  }

  public CalendarFeed         getFullFeed(final CalendarService gcs)
    throws IOException, ServiceException
  {
    return (getServiceFeed(gcs, getFullUrl()));
  }

  public CalendarFeed         getSimpleFeed(final CalendarService gcs)
    throws IOException, ServiceException
  {
    return (getServiceFeed(gcs, getSimpleUrl()));
  }

}
